#include "bsp_led_ctrl.h"
#include "msp_adc.h"

#define LED_QUANTITY                        2                                       // LED指示灯数量

typedef void (*led_ctrl_funtion)(void);

static uint8_t  led_flash_static[LED_QUANTITY]       = {0};                         // led状态
static uint32_t led_flash_base_count[LED_QUANTITY]   = {0};                         // 状态机执行计数
static uint32_t led_flash_flip_value[LED_QUANTITY]   = {0};                         // 灯光反转值
static uint8_t  led_flash_double_count[LED_QUANTITY] = {0};                         // led双闪计数

// 下发命令解析列表
const led_ctrl_funtion led_ctrl_list[LED_QUANTITY][2] =
{
    {Blue_Led_Turn_On, Blue_Led_Turn_Off},
    {Gree_Led_Turn_On, Gree_Led_Turn_Off},
};

//系统状态LED指示灯闪烁模式设置
void Bsp_Led_System_Set(led_list_t led_num, led_flash_mode_t mode)
{
    switch((uint32_t)mode)
    {
        case none_flash:
        {
            led_flash_static[led_num] = 0;
            led_flash_base_count[led_num] = 0;
            led_flash_flip_value[led_num] = 0;
            led_flash_double_count[led_num] = 0;
            break;
        }
        case slow_flash:
        {
            led_flash_static[led_num] = 0;
            led_flash_base_count[led_num] = 0;
            led_flash_flip_value[led_num] = LED_FLASH_SPEED_SLOW;
            led_flash_double_count[led_num] = 0;
            break;
        }
        case fast_flash:
        {
            led_flash_static[led_num] = 0;
            led_flash_base_count[led_num] = 0;
            led_flash_flip_value[led_num] = LED_FLASH_SPEED_FAST;
            led_flash_double_count[led_num] = 0;
            break;
        }
        case double_flash:
        {
            led_flash_static[led_num] = 0;
            led_flash_base_count[led_num] = 0;
            led_flash_flip_value[led_num] = LED_FLASH_SPEED_DOUBLE;
            led_flash_double_count[led_num] = 1;
            break;
        }
    }
}

//系统状态LED状态机
//参数为状态机函数两次运行的时间差，单位毫秒
void Bsp_Led_System_Fsm(uint8_t ms)
{
    for (uint8_t led_num = 0; led_num < LED_QUANTITY; led_num++)
    {
        //计算当前led状态
        if(!led_flash_flip_value[led_num])
            led_flash_static[led_num] = 1;//led常亮
        else
        {
            //生成闪烁效果
            led_flash_base_count[led_num] += ms;
            if(led_flash_base_count[led_num] > led_flash_flip_value[led_num])
            {
                //电平翻转生成闪烁效果
                led_flash_base_count[led_num] = 0;
                led_flash_static[led_num] = ~led_flash_static[led_num];
                //生成双闪效果
                if(led_flash_double_count[led_num])
                {
                    led_flash_double_count[led_num] ++;
                    if(led_flash_double_count[led_num] > 4)
                        led_flash_static[led_num] = 0;
                    if(led_flash_double_count[led_num] > 10)
                        led_flash_double_count[led_num] = 1;
                }
            }
        }
        //输出led状态
        if(led_flash_static[led_num]) led_ctrl_list[led_num][0]();
        else led_ctrl_list[led_num][1]();
    }
}
